package com.lwg.practice.java8.behaviparam.stream;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @FileName: FilterMap.java
 * @Description: All rights Reserved, Designed By Sunyard
 * Copyright: Copyright(C) 2020-2022
 * Company  : Sunyard.hangzhou
 * @author: 李炜光
 * @slogan:
 * @Date: 2021/12/30 0030
 */
public class FilterMap {

    public static void main(String[] args) {

       /* Arrays.stream(words);
        List<Integer> collect = words.stream()
                .map(words->words.split(""))
                .collect(Collectors.toList());
        System.out.println(collect);*/
        //String[] arraywords = {"hello","world"};
     /*   //数组流
        List<String> collect = Arrays.stream(arraywords)
                .map(w -> w.split(""))
                //扁平化映射处理数组流
                .flatMap(Arrays::stream)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(collect);*/


        List<String> words = Arrays.asList("hello", "world");
       /* List<Stream<String>> collect = words.stream()
                .map(word -> word.split(""))
                .map(Arrays::stream)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(collect);*/


        //扁平化
        List<String[]> collect = words.stream()
                .map(word -> word.split(""))
                .distinct()
                .collect(Collectors.toList());
        //System.out.println(collect);


        //生成平方数
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
        List<Integer> collect1 = numbers.stream()
                .map(n -> n * n)
                .collect(Collectors.toList());
        //System.out.println(collect1);


        //组合数组
        List<Integer> numbers1 = Arrays.asList(1, 2, 3);
        List<Integer> numbers2 = Arrays.asList(3, 4);
        List<int[]> collect2 = numbers1.stream()
                .flatMap(i -> numbers2.stream()
                        .map(j -> new int[]{i, j}))
                .collect(Collectors.toList());
        //System.out.println(collect2.stream());


        List<int[]> collect3 = numbers1.stream()
                .flatMap(i ->
                        numbers2.stream()
                                .filter(j -> (i + j) % 3 == 0)
                                .map(j -> new int[]{i, j})
                )
                .collect(Collectors.toList());
        //System.out.println(Arrays.asList(collect3));

        //找到流中的第一个元素
        List<Integer> someNumbers = Arrays.asList(1, 2, 3, 4, 5);
        Optional<Integer> first = someNumbers.stream()
                .map(n -> n * n)
                .filter(n -> n % 2 == 0)
                .findAny();
        System.out.println(first);
    }
}